//
//  DDCartManager.m
//  Dingdong
//
//  Created by 程强 on 16/12/10.
//  Copyright © 2016年 程强. All rights reserved.
//

#import "DDCartManager.h"
#import "DDCartSet.h"

@implementation DDCartManager
- (DSASubject *)addCart:(NSNumber *)goods_id num:(NSString *)num name:(NSString *)name store_id:(NSNumber *)store_id addon:(NSString *)addon product_id:(NSNumber *)product_id weight:(NSNumber *)weight
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/addCart.do", ServerUrl];
    
    NSDictionary *body = @{@"goods_id":FormatString(@"%@", goods_id),@"num":FormatString(@"%@", num),@"name":name,@"store_id":FormatString(@"%@", store_id),@"addon":addon,@"product_id":FormatString(@"%@", product_id),@"weight":FormatString(@"%@", weight)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        [subject sendNext:rdic];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getCartList
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/getCartList.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)getCartNum
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/getNum.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        NSNumber *num = rdic[@"data"];
        [subject sendNext:num];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)cancelSelectedCart:(NSNumber *)cart_id is_check:(NSInteger)is_check
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/cancel.do", ServerUrl];
    
    NSDictionary *body = @{@"cart_id":cart_id,@"is_check":@(is_check)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)cancelSelectedStore:(NSNumber *)store_id is_check:(NSInteger)is_check
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/cancel.do", ServerUrl];
    
    NSDictionary *body = @{@"store_id":store_id,@"is_check":@(is_check)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)cancelSelectedAll:(NSInteger)is_checkAll
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/cancel.do", ServerUrl];
    
    NSDictionary *body = @{@"is_checkAll":@(is_checkAll)};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)deleteCartObj:(NSString *)cart_id
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/delete.do", ServerUrl];
    
    NSDictionary *body = @{@"cart_id":cart_id};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)addCartObjNum:(NSNumber *)cart_id num:(NSString *)num
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/addCount.do", ServerUrl];
    
    NSDictionary *body = @{@"cart_id":cart_id,@"num":num};
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)clearCart
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/cart/clean.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
- (DSASubject *)cartValidCheck
{
    DSASubject *subject = [DSASubject subject];
    NSString *doVerifyurl=[NSString stringWithFormat:@"%@wap/order/invalid_cart.do", ServerUrl];
    
    NSDictionary *body = nil;
    NSString *sign = [SystemConfig httpSignWithBody:body];
    
    [KKHttpServices httpPostUrl:doVerifyurl sign:sign prams:body success:^(id responseObject, KKHttpParse *parse) {
        NSDictionary *rdic = parse.responseJsonOB;
        DDCartSet *set = [DDCartSet modelWithDictionary:rdic];
        [subject sendNext:set];
        [subject sendCompleted];
    } failure:^(KKHttpParse *parse) {
        NSError* err = nil;
        [KKHttpServices creatHttpErr:parse err:&err];
        [subject sendError:err];
        [subject sendCompleted];
    } cancel:^(NSURLSessionDataTask *task) {
        __strong DSASubject *dsaSubject = subject;
        [subject setDispose:^{
            [task cancel];
            [dsaSubject sendCompleted];
        }];
    }];
    return subject;
}
@end
